const db = require("../lib/conn_oracle")

//用户登录日志
let loginLogSql = `insert into c##wxw.vue_login_log(id,user_id,event_time,client_ip,action_type,status)
                    values('',:personid,sysdate,:clientIp,:actionType,:status)`
// 用户session
let sessionSql = `insert into c##wxw.vue_user_sessions(session_id,user_id,token_hash,client_ip,login_time,last_activity,expiry_time,is_active)
                    values(:sessionId,:personId,:tokenHash,:clientIp,sysdate,sysdate,to_timestamp(:expiryTime,'yyyy-mm-dd hh24:mi:ss.ff'),:isActive)`

//session查询
let sessionSearchSql = `select * from c##wxw.vue_user_sessions 
                        where is_active = 1 
                          and user_id = :personId
                          and client_ip = :clientIp
                          and expiry_time = to_timestamp(:ExpiryTime,'yyyy-mm-dd hh24:mi:ss.ff')                         
                        `

//session更新(退出登录、强制退出登录)
let sessionLogoutSql = `update c##wxw.vue_user_sessions
                        set is_active = 0
                        where session_id = :sessionId
                        `

//token有效性校验
let tokenHashCheckSql = `select session_id,token_hash from c##wxw.vue_user_sessions
                        where is_active = 1
                          and user_id = :personId
                          and client_ip = :clientIp
                          and expiry_time = to_timestamp(:expiryTime,'yyyy-mm-dd hh24:mi:ss.ff')
                        `

//更新session的token_hash，exptry_time
let updateSessionSql = `update c##wxw.vue_user_sessions
                        set token_hash = :tokenHash,
                            last_activity = sysdate,
                            expiry_time = to_timestamp(:expiryTime,'yyyy-mm-dd hh24:mi:ss.ff')
                        where session_id = :sessionId
                        `

//
let ops = {autoCommit:true}

/**
 * 登录成功时写入登录日志和session
 * @param {Array} lDatas 登录日志数据
 * @param {Array} sDatas 会话数据
 */
exports.addLogAndSession = async (lDatas,sDatas) => {
    await db.writeTab(loginLogSql,lDatas)
    await db.writeTab(sessionSql,sDatas) 
}

/**
 * 登录失败时只写入登录日志，不写入session，异步操作，不等待
 * @param {Array} lDatas 登录日志数据 
 */
exports.addLog = (lDatas) => {
    db.writeTab(loginLogSql,lDatas)
}

exports.manyAddLog = (lDatas) => {
    db.writeManyTab(loginLogSql,lDatas,ops) 
}

/**
 * 查询session
 * @param {Array} datas 用户PERSONID\CLIENTIP\EXPIRETTIME
 * @returns 
 */
exports.searchSession = (datas) => {
    return db.search(sessionSearchSql,datas)
}

/**
 * 更新session状态（退出登录、强制退出登录）
 * @param {*} sessionid 
 * @returns 
 */
exports.logoutSession = (sessionid) => {
    return db.writeTab(sessionLogoutSql,sessionid)
}

exports.manyLogoutSession = (sessionids) => {
    return db.writeManyTab(sessionLogoutSql,sessionids,ops)
}

/**
 * token续期时，查出会话表存储的TOKEN
 * @param {*} datas 
 * @returns 
 */
exports.tokenHashCheck = async (datas) => {
    return await db.search(tokenHashCheckSql,datas)
}

/**
 * token续期后，更新会话表的token_hash，last_activity，expiry_time
 * @param {*} datas 
 * @returns 
 */
exports.updateSession = (datas) => {
    return db.writeTab(updateSessionSql,datas)
}